/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var rob = function (root) {
    // 选与不选
    // 思路队列：但是执行超时
    const dfs = (node) => {
        if(node === null) return 0
        let chooseNode = node.val
        if (node.left) {
            chooseNode += dfs(node.left.left)
            chooseNode += dfs(node.left.right)

        }
        if (node.right) {
            chooseNode += dfs(node.right.left)
            chooseNode += dfs(node.right.right)
        }
        return Math.max(dfs(node.left) + dfs(node.right), chooseNode)
    }
    return dfs(root)
};
var rob1 = function (root) {
    // 选与不选
    const dfs = (node) => {
        // 不管选不选还，都是0
        if(node === null) return [0, 0]
        const [lRob, lNotRob] = dfs(node.left)
        const [rRob, rNotRob] = dfs(node.right)
        const rob = lNotRob + rNotRob + node.val
        const notRob = Math.max(lRob, lNotRob) + Math.max(rRob, rNotRob)
        return [rob, notRob]
    }
    return Math.max(...dfs(root))
};